package com.zzuli.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zzuli.practice.entity.Comment;
import com.zzuli.practice.entity.Post;
import com.zzuli.practice.mapper.CommentMapper;
import com.zzuli.practice.service.ICommentService;
import com.zzuli.practice.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.sql.Wrapper;
import java.util.List;

@Service
public class CommentService implements ICommentService {

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private CommentMapper commentMapper;

    public List<Comment> selectCommentByPostId(Integer postid){
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("postid", postid);
        return commentMapper.selectList(wrapper);
    }

    public int insertComment(Comment comment){

        // html标签转义
        comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));

        // 敏感词过滤
        comment.setContent(sensitiveFilter.filter(comment.getContent()));

        return commentMapper.insert(comment);
    }

    public int updateHeaderImg(String username, String headerimg){
        UpdateWrapper<Comment> wrapper = new UpdateWrapper<>();
        wrapper.eq("username", username);
        wrapper.set("headerimg", headerimg);
        return commentMapper.update(null, wrapper);
    }

}
